package com.yima.baseproj.module.app.resource;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.module.app.resource.model.AppResourceAddParamModel;
import com.yima.baseproj.module.app.resource.model.AppResourceEditParamModel;
import com.yima.baseproj.module.app.resource.model.AppResourceSearchModel;
import com.yima.baseproj.config.application.ApplicationUtil;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.app.resource.model.*;
import com.yima.baseproj.security.SecurityUtil;
import com.yima.baseproj.security.annotation.SecurityIgnoreUrl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Slf4j
@Api(tags = "资源管理")
@RestController
@RequestMapping("api/app/resource")
public class AppResourceController extends BaseController {

    @Resource
    private AppResourceService appResourceService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public Object findAllByPage(PageVo page, AppResourceSearchModel searchVo) {
        Page<Map> pageData = appResourceService.findByPage(PageUtil.initMpPage(page),searchVo);
        for (Map record : pageData.getRecords()) {
            record.put("urlUsing",String.format("%s/api/app/resource/type/%s", ApplicationUtil.baseUrl,record.get("type").toString()));
        }
        return pageData;
    }

    @ApiOperation("查询列表")
    @GetMapping(value = "/findAllByList")
    public Object findAllByList(AppResourceSearchModel searchVo) {
        String userId = SecurityUtil.getUserId();
        searchVo.setUserId(userId);
        List<Map> list = appResourceService.findByList(searchVo);
        return list;
    }

    @ApiOperation("查询详情")
    @GetMapping(value = "/findById/{id}")
    public Map findById(@PathVariable @NotBlank(message = "参数不能为空")String id) {
        AppResourceEntity appResourceEntity = appResourceService.getById(id);
        if(appResourceEntity == null){
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }

        Map result = JSON.parseObject(JSON.toJSONString(appResourceEntity),Map.class);

        return result;
    }

    @ApiOperation("查询通过类型")
    @SecurityIgnoreUrl
    @GetMapping(value = "/findByType/{type}")
    public void findByType(@PathVariable String type, HttpServletResponse response) throws IOException {
        AppResourceEntity appResourceEntity = appResourceService.findByType(type);
        if(appResourceEntity == null){
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }
        response.sendRedirect(appResourceEntity.getUrl());
    }

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody AppResourceAddParamModel paramModel) {
        //方式一：适合单一添加
        AppResourceEntity appResourceEntity = new AppResourceEntity();
        appResourceEntity.setData(paramModel);
        appResourceService.save(appResourceEntity);

        //方法二：适合事务添加
//        appResourceService.add(paramModel);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/edit")
    public void edit(@Validated @RequestBody AppResourceEditParamModel paramModel) {
        //校验参数
        AppResourceEntity appResourceEntity = appResourceService.getById(paramModel.getId());
        if(appResourceEntity == null){
            throw  new ApiException("数据不存在，请刷新重试");
        }
        appResourceEntity.setData(paramModel);
        appResourceService.saveOrUpdate(appResourceEntity);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delById(@PathVariable String[] ids) {
        appResourceService.removeByIds(Arrays.asList(ids));
    }
}